import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private repository: Repository<User>,
  ) {}

  /**
   * 登录
   * @param username
   * @param password
   * @returns
   */
  login(username: string, password: string): Promise<User> {
    return this.repository.findOne({where:{username: username, password: password}});
  }

  findOne(id: number): Promise<User> {
    if(!id){
      return undefined
    }
    return this.repository.findOne({where:{id: id}});
  }

  //查询用户的wx_user
  async findWxUser(token: number): Promise<{wx_user?: string, error?: string}>{
    let findUser:User = await this.findOne(token)
    if(!findUser){
      return {error:'用户不存在'}
    }
    return {wx_user:findUser.wx_user}
  }


}
